package dom;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;

public class XMLStudentAddAndDelAndUpd_Dom4j {
    Document doc=null;
    public void getDocument(){
        // 获取解析器工厂
        DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();

        // 获取解析器
        try {
            SAXReader saxReader=new SAXReader();
            doc=saxReader.read(new File("src/main/resources/student.xml"));
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    public void showInfo(){
        // 获取文档的根元素
        Element root=doc.getRootElement();
        // 添加数据
        Element student = root.addElement("student");
        student.addAttribute("id","3");
        Element studentName = student.addElement("name");
        studentName.addText("老六");

        Element course = student.addElement("course");
        course.addText("语文");

        Element score = student.addElement("score");
        score.addText("57");

        // 修改数据
        for (Iterator it=root.elementIterator();it.hasNext();){
            Element element=(Element) it.next();

            if(element.attributeValue("id").equals("2")){
                for (Iterator itType=element.elementIterator();itType.hasNext();){
                    Element typeEle=(Element) itType.next();
                    String name=typeEle.getName();
                    if(name.equals("score")){
                        typeEle.setText("60");
                        System.out.println("\t分数："+typeEle.getText());
                    }
                }
            }
        }

        // 数据显示 （简答4）
        for (Iterator iterable = root.elementIterator(); iterable.hasNext();){
            Element brandEle=(Element) iterable.next();

            System.out.println("学号："+brandEle.attributeValue("id"));
            for (Iterator itType=brandEle.elementIterator();itType.hasNext();){
                Element typeEle=(Element) itType.next();
                String name=typeEle.getName();
                if(name.equals("name")){
                    System.out.println("\t姓名："+typeEle.getText());
                }
                if(name.equals("course")){
                    System.out.println("\t学科："+typeEle.getText());
                }
                if(name.equals("score")){
                    System.out.println("\t分数："+typeEle.getText());
                }
            }
        }

//         删除数据
        for (Iterator it=root.elementIterator();it.hasNext();){
            Element element=(Element) it.next();
            String name = element.attributeValue("id");
            if(name.equals("1")){
                root.remove(element);
                break;
            }
        }
       //保存信息
        OutputFormat of= OutputFormat.createPrettyPrint();
        of.setEncoding("UTF-8");
        try {
            XMLWriter writer=new XMLWriter(new FileWriter("src/main/resources/student.xml"),of);
            writer.write(doc);
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        XMLStudentAddAndDelAndUpd_Dom4j phoneAddAndDelAndUpd=new XMLStudentAddAndDelAndUpd_Dom4j();
        phoneAddAndDelAndUpd.getDocument();
        phoneAddAndDelAndUpd.showInfo();
    }
}
